import 'package:flutter/material.dart';
import 'package:flutterapp/chapter/eight/event_bus.dart';

/// @date 2020/08/06  15:15
/// @author 程志强
/// @des eventBus

class EventBusRoute extends StatefulWidget {
  @override
  _EventBusRouteState createState() => _EventBusRouteState();
}

class _EventBusRouteState extends State<EventBusRoute> {
  var userName;

  @override
  Widget build(BuildContext context) {
    bus.on("login", (arg) {
      setState(() {
        userName = arg;
      });
      print("==EventBusRoute1===$arg");
    });
    return Scaffold(
      appBar: AppBar(
        title: Text("事件总线A"),
      ),
      body: Center(
        child: RaisedButton(
          child: Text("去B页面触发事件__${userName??""}"),
          onPressed: () => Navigator.of(context)
              .push(MaterialPageRoute(builder: (context) => EventBusRoute2())),
        ),
      ),
    );
  }
}

class EventBusRoute2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("事件总线B"),
      ),
      body: Center(
        child: RaisedButton(
          child: Text("触发事件"),
          onPressed: () {
            bus.emit("login", "张三");
          },
        ),
      ),
    );
  }
}
